﻿/*******************************************************************************
 * Copyright © 2016 DJY 版权所有
 * Author: DJY
 * Description: 后台管理系统
 * Website：http://www.djyweb.cn
*********************************************************************************/
using System;
using System.Web.Mvc;
using DJY.Domain.Entity.SystemManage;
using DJY.Domain.Entity.SystemSecurity;
using DJY.Code;
using DJY.Code.Enums;
using DJY.Service.SystemSecurity;
using DJY.Service.SystemManage;
using System.Data.Entity.Core;

namespace DJY.Web.Controllers
{
    public class LoginController : Controller
    {

        [HttpGet]
        public virtual ActionResult Index()
        {
            var user = OperatorProvider.GetCurrent();
            if (user != null && !string.IsNullOrEmpty(user.UserId))
            {
                return RedirectToAction("Index", "Home");
            }

            return View();
        }

        [HttpGet]
        public ActionResult GetAuthCode()
        {
            return File(new VerifyCode().GetVerifyCode(), @"image/gif");
        }

        [HttpGet]
        public ActionResult OutLogin()
        {
            OperatorModel user = OperatorProvider.GetCurrent();
            OperatorProvider.RemoveCurrent();

            OperatorProvider.Resolve<ISysUserService>().UserLogout(user.UserId);

            Session.Abandon();
            Session.Clear();
            
            return RedirectToAction("Index", "Login");
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult CheckLogin(string username, string password, string code = "")
        {
            try
            {
                if (Session["DJY_session_verifycode"].IsEmpty() || Md5.md5(code.ToLower(), 16) != Session["DJY_session_verifycode"].ToString())
                {
                    return Json(AjaxResult.Fail("验证码错误，请重新输入"));
                }

                Session["DJY_session_verifycode"] = null;
                SysUserEntity userEntity = OperatorProvider.Resolve<ISysUserService>().CheckLogin(username, password);
                OperatorModel operatorModel = new OperatorModel();
                operatorModel.UserId = userEntity.F_Id;
                operatorModel.UserCode = userEntity.F_Account;
                operatorModel.UserName = userEntity.F_RealName;
                operatorModel.HeaderPic = userEntity.F_HeadIcon;
                operatorModel.CompanyId = userEntity.F_OrganizeId;
                operatorModel.CompanyName = userEntity.Organize;
                operatorModel.DepartmentId = userEntity.F_DepartmentId;
                operatorModel.Department = userEntity.Department;
                operatorModel.DutyId = userEntity.F_DutyId;
                operatorModel.Duty = userEntity.Duty;
                operatorModel.RoleId = userEntity.F_RoleId;
                operatorModel.Role = userEntity.Role;
                operatorModel.LoginIPAddress = NetNet.Ip;
                operatorModel.LoginIPAddressName = NetNet.GetLocation(operatorModel.LoginIPAddress);
                operatorModel.LoginTime = DateTime.Now;
                operatorModel.LoginToken = Guid.NewGuid().ToString("N");
                operatorModel.UserType = userEntity.F_UserType;
                OperatorProvider.AddCurrent(operatorModel);
                return Json(AjaxResult.OK("登录成功。"));
            }
            catch (EntityException ex)
            {
                Logs.Write(ex.ToString());
                return Json(AjaxResult.Fail(Common.ShowException(ex)));
            }
            catch (Exception ex)
            {
                SysLogEntity log = new SysLogEntity();
                log.F_Id = Guid.NewGuid().ToString();
                log.F_Date = DateTime.Now;
                log.F_IPAddress = NetNet.Ip;
                log.F_IPAddressName = NetNet.GetLocation(log.F_IPAddress);
                log.F_Type = DbLogType.Login.ToString();
                log.F_CreatorTime = DateTime.Now;
                log.F_CreatorUserId = string.Empty;
                log.F_Account = username;
                log.F_Description = Common.ShowException(ex);
                log.F_Result = false;
                OperatorProvider.Resolve<ISysLogService>().InsertLog(log);
                return Json(AjaxResult.Fail(Common.ShowException(ex)));
            }
        }
    }
}
